<!DOCTYPE html>
<html lang="zh_CN" class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>定时任务</title>
    <header th:replace="header::html"></header>
</head>
<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">任务</a>
        <a>
          <cite>任务列表</cite></a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>

<div class="x-body">
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body" permission="sys:job:query">
                        <form class="layui-form layui-col-space5">
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" name="beanName" placeholder="请输入Bean名" autocomplete="off"
                                       class="layui-input">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" lay-submit="" lay-filter="search"><i
                                        class="layui-icon">&#xe615;</i></button>
                            </div>
                        </form>
                    </div>
                    <div class="layui-card-body ">
                        <table class="layui-hide" id="table" lay-filter="member"></table>
                    </div>
                    <div class="layui-card-body ">
                        <script type="text/html" id="barDemo">
                            <a class="layui-btn layui-btn-xs" lay-event="edit" permission="sys:job:edit"><i
                                    class="layui-icon">&#xe642;</i>编辑</a>
                            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"
                               permission="sys:job:del"><i class="layui-icon">&#xe640;</i>删除</a>
                        </script>
                    </div>
                    <div class="layui-card-body">
                        <script type="text/html" id="toolbarDemo">
                            <div class="layui-btn-container">
                                <button class="layui-btn" onclick="xadmin.open('添加任务','/job/add')"
                                        permission="sys:job:add"><i class="layui-icon">&#xe654;</i>添加任务
                                </button>
                            </div>
                        </script>
                    </div>
                    <div class="layui-card-body">
                        <script type="text/html" id="statusTpl">
                            <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启动|禁用"
                                   lay-filter="sexDemo" {{ d.status == 1 ? 'checked' : '' }}>
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script type="text/javascript">
    layui.use(['table', 'layer', 'form'], function () {
        $ = layui.jquery;
        var table = layui.table,
            layer = layui.layer,
            form = layui.form;
        var tableIns = table.render({
            elem: '#table'
            , url: '/job/listPage' //数据接口
            , page: true //开启分页
            , toolbar: '#toolbarDemo'
            , response: {
                dataName: 'datas' //规定数据列表的字段名称，默认：data
            }
            , cols: [
                [
                    {field: 'id', title: 'ID', sort: true, width: 80, align: "center"},
                    {field: 'beanName', title: 'bean名'},
                    {field: 'methodName', title: '方法名'},
                    {field: 'methodParam', title: '方法参数'},
                    {field: 'cron', title: 'cron表达式'},
                    {field: 'remark', title: '备注'},
                    {field: 'status', title: '状态', align: "center", templet: '#statusTpl'},
                    {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 180}
                ]
            ]
            , done: function () {
                checkPermission();
            }
        });

        //监听工具条
        table.on('tool(member)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么?', function (index) {
                    $.ajax({
                        url: "/job/deleteJob",
                        type: "GET",
                        data: {id: data.id},
                        dataType: 'json',
                        success: function (result) {
                            if (result.code == 200) {
                                layer.alert("删除成功", {icon: 1}, function (index1) {
                                    layer.close(index1);
                                    tableIns.reload();
                                });
                            } else {
                                layer.alert(result.msg);
                            }
                        },
                    });
                });
            } else if (obj.event === 'edit') {
                xadmin.open('编辑文件', '/job/edit/?id=' + data.id);
            }
        })

        function md(id, status) {
            $.ajax({
                url: "/job/mdJob",
                type: "POST",
                data: {id: id, status: status},
                dataType: 'json',
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg("设置成功", {icon: 6});
                    } else {
                        layer.msg(result.msg);
                    }
                },
            });
        }

        //监听状态操作
        form.on('switch(sexDemo)', function (obj) {
            md(obj.value, obj.elem.checked ? 1 : 2);
        });

        form.on('submit(search)', function (data) {
            var beanName = data.field.beanName;
            tableIns.reload({
                url: "/job/listPageByInfo",
                where: {
                    "beanName": beanName
                },
                page: {curr: 1}
            })
            return false;
        });
        checkPermission();
    });
</script>
